package com.sysu.simulation.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.sysu.simulation.widget.BaseRecyclerFragment;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter{
    private final int NORMAL_VIEW = 0;
    private final int FOOTER_VIEW = 1;
    private final int BEHAVIOR_MODE;
    private final int NORMAL = 0;
    private final int WITH_FOOTER = 1;

    protected List<T> mItems;
    protected Context mContext;
    protected LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;
    private OnClickListener onClickListener;
    private OnLongClickListener onLongClickListener;

    public BaseRecyclerAdapter(Context context, int mode) {
        mItems = new ArrayList<>();
        this.mContext = context;
        this.mInflater = LayoutInflater.from(context);
        BEHAVIOR_MODE = mode;
        initListener();
    }

    private void initListener() {
        onClickListener = new OnClickListener() {
            @Override
            public void onClick(int position, long itemId) {
                if (onItemClickListener != null)
                    onItemClickListener.onItemClick(position, itemId);
            }
        };

        onLongClickListener = new OnLongClickListener() {
            @Override
            public boolean onLongClick(int position, long itemId) {
                if (onItemLongClickListener != null) {
                    onItemLongClickListener.onItemLongClick(position, itemId);
                    return true;
                }
                return false;
            }
        };
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case FOOTER_VIEW:
                return onCreateFooterViewHolder(parent);
            default:
                RecyclerView.ViewHolder holder = onCreateDefaultViewHolder(parent, viewType);
                if (holder != null) {
                    holder.itemView.setTag(holder);
                    holder.itemView.setOnLongClickListener(onLongClickListener);
                    holder.itemView.setOnClickListener(onClickListener);
                }
                return holder;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case FOOTER_VIEW:
                onBindFooterViewHolder(holder);
                break;
            default:
                onBindDefaultViewHolder(holder, mItems.get(position), position);
                break;
        }
    }

    protected abstract RecyclerView.ViewHolder onCreateDefaultViewHolder(ViewGroup parent, int type);

    protected RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        return null;
    }

    protected abstract void onBindDefaultViewHolder(RecyclerView.ViewHolder holder, T item, int position);

    protected void onBindFooterViewHolder(RecyclerView.ViewHolder holder) {}

    @Override
    public int getItemViewType(int position) {
        if ((BEHAVIOR_MODE == WITH_FOOTER) && (position + 1 == getItemCount()))
            return FOOTER_VIEW;
        else return NORMAL_VIEW;
    }

    @Override
    public int getItemCount() {
        if(BEHAVIOR_MODE == WITH_FOOTER) return mItems.size()+1;
        else return mItems.size();
    }

    public T getItem(int position) {
        return mItems.get(position);
    }

    public void addItem(T item) {
        if(item != null) {
            mItems.add(0, item);
            notifyItemInserted(0);
        }
    }

    public void addItems(List<T> items) {
        if(items != null) {
            mItems.addAll(items);
            notifyItemRangeInserted(mItems.size(), items.size());
        }
    }

    public void setItem(int position, T item) {
        if(item != null) {
            mItems.set(position, item);
            notifyItemChanged(position);
        }
    }

    public void removeItems() {

    }

    public void clear() {
        mItems.clear();
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public static abstract class OnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
            onClick(holder.getAdapterPosition(), holder.getItemId());
        }

        public abstract void onClick(int position, long itemId);
    }

    public static abstract class OnLongClickListener implements View.OnLongClickListener {
        @Override
        public boolean onLongClick(View v) {
            RecyclerView.ViewHolder holder = (RecyclerView.ViewHolder) v.getTag();
            return onLongClick(holder.getAdapterPosition(), holder.getItemId());
        }

        public abstract boolean onLongClick(int position, long itemId);
    }

    public interface OnItemClickListener {
        void onItemClick(int position, long itemId);
    }

    public interface OnItemLongClickListener {
        void onItemLongClick(int position, long itemId);
    }
}
